package A05_栈;

/**
 * @Author: KingWang
 * @Date: 2021/6/1
 * @Desc:
 **/
public class Stack<T> {

    private int maxSize = 10;
    private int top = -1;
    private T[] arr = null;

    public Stack(int size){
        this.maxSize = size;
        arr = (T[])new Object[maxSize];
    }

    public void push(T a){
        if(isFull()){
            System.out.println("栈空间已满");
            return;
        }
        top++;
//        System.out.println(top + "->" + a);
        arr[top] = a;
    }

    public T pop(){
        if(isEmpty()){
            System.out.println("栈空间已空");
            return null;
        }
        T temp = arr[top];
        System.out.println("弹出 -> " + temp);
        top--;
        return temp;
    }

    public void show(){
        if(isEmpty()){
            System.out.println("栈为空");
            return;
        }

        for (int i = top; i >=0 ; i--) {
            System.out.println("["+i+"]:" + arr[i]);
        }
    }
    public boolean isFull(){
        return top >= maxSize -1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

}
